   ***data paths 
   global archive /Volumes/Sans titre/DATA_MICROEU
   global posted /Volumes/Sans titre/DATA_MICROEU/Posting
   global data /Users/Mathilde/Dropbox/Posted_workers/Data
  
   
   
   **output paths
  global replication_outputs_a /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Appendix_figures
  global replication_estimates /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Main_estimates
  global replication_estimates_a /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Appendix_estimates
  global replication_outputs /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Main_figures
  

   
   
   ********************************************************************
   * Replication file for Figure 2 of the paper
   * + supplementary figures b12, b8,b7 and table b5 
   ****************************************************************
   
   
   
  *********************************
  *1. Standard Diff-Diff model 
  *********************************
 
*Import dataset appending posting data collected for four main destination countries: France (DPD), Germany (SOKA-BAU), Austria (BUAK),   Belgium   (LIMOSA). Each country provided files in different formats (excels, pdf, paper..). The consolidated dataset has one line by destination-origin, with dummies to identify origin countries in NMS1, NMS2, NMS3 and EVENT is year of liberalization for each country-pair (=0 if not affected)
 
 import excel "$data/EUaccession_ES.xlsx", sheet("all2") firstrow clear
 *variable "country" is origin, variable "destination" is destination
 tab country 
 *checking no aggregate variable has been left
 drop if country=="total"
 
 *create dummy=1 if the country-pair experiences a liberalization event (EVENT!=0)
 g treated=0
 replace treated=1 if EVENT>0
 
 destring n*, replace force
 reshape long n, i(country destination NMS1 NMS2 NMS3 treated EVENT) j(year)
 
 g time=year-EVENT
 replace time=0 if treated==0
 sum time if treated==1
 global min=abs(r(min))
 
 g timed=time+$min
 replace timed=0 if treated==0
 su timed
 global max=abs(r(max))
 
 forval y=0/$max{
 g coeff_`y'=0
 replace coeff_`y'=1 if treated==1 & timed==`y'
 }
 
forval y=0/$max{
local j=`y'-$min
label var coeff_`y' "`j'"
}

  *binning Event-Time Coefficients 
  g coeff_bottom=0
  replace coeff_bottom=1 if treated==1 & time<-4

  g coeff_up=0
  replace coeff_up=1 if treated==1 & time>8
  
  *Outcome variable: log bilateral posting flows (number of posting contracts)
  g logposted=log(n)
  
  *groups for fixed effects 
  egen pair=group(destination country)
  egen countrynum=group(country)
  egen destinationnum=group(destination)


   g zero=1
   label var zero "-1"
   
   *no duplicates
   duplicates tag destination country year, g(flag)
   tab flag

   foreach y in AT DE FR BE{ 
   egen unique_`y'=group(country) if destination=="`y'"
   su unique_`y'
   }

   *Creating the interaction dummy for the 2x2 model 
   g interaction=0
   replace interaction=1 if treated==1 & time>-1

   *2x2 model with only year and paid FE
   reghdfe logposted  i.interaction, absorb(year pair) cluster(pair)
   outreg2 using "$replication_estimates_a/TableB5.xls", replace
   global beta=round(_b[1.interaction], 0.01)
   global se=round(_se[1.interaction], 0.01)
   
   tab coeff_bottom coeff_10 
   tab coeff_bottom coeff_22 
   
   
   ********Figure 2, regressions *****************
   
   reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year) cl(pair)
   est store DD
   
   coefplot, keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22) omit vertical ///
   graphregion(fcolor(white) lcolor(white))   ciopts(recast(rcap) lwidth(medthick)) ///
   ylabel(-4 (2) 4) xline(3.5, lcolor(red)) yline(0, lcolor(gs10) lpattern(dash)) xtitle("Years to Posting Liberalization") ytitle(Log Posted Workers Flows) 
   
   postutil clear 
   postfile results str32 varname b se using "$replication_estimates/DD.dta", replace
local regression_vars coeff_10  coeff_11 coeff_12 zero coeff_14 coeff_15 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22
foreach r of local regression_vars {
    post results ("`r'") (_b[`r']) (_se[`r'])
}
  postclose results
   su n if time==-1 & treated==1
   
  
   *Alternative specification: pure event-study focusing only on treated countries 
   reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up if treated==1, absorb(pair year ) cl(pair)
   est store DD_ES
   
   *adding destination-year FE
   reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair destinationnum##year) cl(countrynum)
   est store DDD1
   
   *adding origin-year + destination year FE 
   reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair destinationnum##year countrynum##year) cl(pair)
   est store DDD
   postutil clear 
   postfile results str32 varname b se using "$replication_estimates/DDD.dta", replace
local regression_vars coeff_10  coeff_11 coeff_12 zero coeff_14 coeff_15 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22
foreach r of local regression_vars {
    post results ("`r'") (_b[`r']) (_se[`r'])
}
  postclose results

   
   coefplot (DD,  keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22) label("Baseline")) ///
   (DDD,  keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22 ) label("Origin-Year and Destination-Year FE")), ///
   omit vertical ///
   graphregion(fcolor(white) lcolor(white))   ciopts(recast(rcap) lwidth(medthick))  ///
   xline(3.5, lcolor(red)) yline(0, lcolor(gs10) lpattern(dash)) xtitle("Years to Posting Liberalization") ytitle(Log Flows) ///
   legend(ring(0) pos(11) cols(1)) ylabel(-4 (3) 4)
   save "$data/figure1_regression.dta", replace

   use "$data/figure1_regression.dta", clear
   reghdfe logposted  i.interaction, absorb(destinationnum##year countrynum##year pair) cluster(pair)
   global beta2=round(_b[1.interaction], 0.01)
   global se2=round(_se[1.interaction], 0.01)
   outreg2 using "$replication_estimates_a/TableB5.xls", append
   
   *Robutsness to using the PPMLHDFE regressor 
   ppmlhdfe n i.interaction , absorb(pair countrynum##year destinationnum##year) cl(pair)
   outreg2 using "$replication_estimates_a/TableB5.xls", append  

   
   *Robustness to alternative estimator (did_multiplegt command)
   egen ID=group(destination country EVENT)
   did_multiplegt logposted ID year interaction, placebo(4) dynamic(8) breps(20) robust_dynamic  save_results("$replication_estimates/did")
   ereturn list
   *check 
   *tab pair, g(d)
   *did_multiplegt logposted ID year interaction, placebo(4) dynamic(8) breps(20) cluster(pair) controls(d1-d78) robust_dynamic
   *ereturn list
   
   *this step is necessary because past version of the did_multiplegt command was
   *plotting graphs with placebo0=coefficient for time=-1. To stick to the 
   *archival results, I stick to this version of the command/implementation accessed in 2020. 
   
   ********************Figure 2, final graph*********************
   preserve 
   
   use "$replication_estimates/did.dta", clear 
   drop if time==-1 
   replace time_to_treatment=time_to_treatment+1 if time<0
   save "$replication_estimates/did2.dta"  , replace 
   
   use "$replication_estimates/DD.dta", clear
   g type="DD"
   append using "$replication_estimates/DDD.dta"
   replace type="DDD" if missing(type)
   g time_to_treatment=0
   forval y=10/22{
   local j=`y'-$min
   replace time_to_treatment=`y'-$min if varname=="coeff_`y'"
   }
   replace time_to_treatment=-1 if varname=="zero"
   
   rename se se_treatment_effect
   rename b treatment_effect
   append using "$replication_estimates/did2.dta"
   replace type="HTE" if missing(type)
   
   g lb=treatment_effect-1.96*se_treatment_effect
   g ub=treatment_effect+1.96*se_treatment_effect
   
   
   replace time_to_treatment=time+0.4 if type=="DDD"
   replace time_to_treatment=time+0.2 if type=="HTE"
   rename time_to_treatment time 
   rename treatment_effect b

  
  twoway  ///
(scatter b time if type=="DDD", mcolor(cranberry) lcolor(cranberry) msymbol(T) ) ///
(rcap ub lb time if type=="DDD", lcolor(cranberry) lwidth(medthick) ) ///
(scatter b time if type=="DD", mcolor(dkblue) lcolor(dknavy)) ///
(rcap ub lb time if type=="DD", lcolor(dknavy) lwidth(medthick)) ///
(scatter b time if type=="HTE", mcolor(gs10) lcolor(gs10)) ///
(rcap ub lb time if type=="HTE", lcolor(gs10) lwidth(medthick) lpattern(dash)) ///
, xline(-.5, lcolor(red)) ///
graphregion(fcolor(white) lcolor(white)) ///
ylabel(-4 (2) 4) xlabel(-4 (1) 8) xtitle("") ///
yline(0, lcolor(gs10) lpattern(dash)) ytitle("Log Posting Flows") ///
legend(label(1 "Origin-Year and Destination-Year FE") label(3 "Baseline") label(5 "Heterogenous Treatment Effect Estimator") ring(0) pos(5) ///
  cols(1) order( 3 1 5)) xtitle("Years to Posting Liberalization")  ///
        ttext(-1 2 "Average {&beta}=$beta2($se2)", place(e))  
    graph export "$replication_outputs/Figure2.pdf", replace
  
   
    twoway  ///
(scatter b time if type=="DDD", mcolor(black) lcolor(black) msymbol(T) ) ///
(rcap ub lb time if type=="DDD", lcolor(black) lwidth(medthick) ) ///
(scatter b time if type=="DD", mcolor(dkblue) lcolor(dknavy)) ///
(rcap ub lb time if type=="DD", lcolor(dknavy) lwidth(medthick)) ///
(scatter b time if type=="HTE", mcolor(gs10) lcolor(gs10)) ///
(rcap ub lb time if type=="HTE", lcolor(gs10) lwidth(medthick) lpattern(dash)) ///
, xline(-.5, lcolor(red)) ///
graphregion(fcolor(white) lcolor(white)) ///
ylabel(-4 (2) 4) xlabel(-4 (1) 8) xtitle("") ///
yline(0, lcolor(gs10) lpattern(dash)) ytitle("Log Posting Flows") ///
legend(label(1 "Origin-Year and Destination-Year FE") label(3 "Baseline") label(5 "Heterogenous Treatment Effect Estimator") ring(0) pos(5) ///
  cols(1) order( 3 1 5)) xtitle("Years to Posting Liberalization")  ///
        ttext(-1 2 "Average {&beta}=$beta2($se2)", place(e))  
    graph export "$replication_outputs/Figure2_blackwhite.pdf", replace
	
   restore 
   
***************APPENDIX: Supplementary analysis for Fig 2 and Section 4*****************************

   *********************************************************
   *alternative clustering  (appendix figure)
   *********************************************************
   
      reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year countrynum##year) cl(pair)
	  
	  egen cluster1=group(countrynum year)
	  egen cluster2=group(destinationnum year)
	  
	    reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year countrynum##year) cl(pair)
		est store cl1
		reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year countrynum##year) cl(countrynum)
		est store cl2
		    reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year countrynum##year) cl(cluster1)
		est store cl2
			  reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year countrynum##year) cl(cluster2)
		est store cl3
			  reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year countrynum##year) cl(cluster2 cluster1)
		est store cl4
		reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year countrynum##year) cl(countrynum)
		est store cl5
		
		
   coefplot (cl1, label("Origin-Destination")) ///
   (cl2,  label("Origin-Year")) ///
   (cl3,  label("Destination-Year")) ///
   (cl4,  label("Origin-Year, Destination-Year")) ///
   (cl5,  label("Origin-level cluster")) ///
   ,keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22 ) ///
   omit vertical ///
   graphregion(fcolor(white) lcolor(white))   ciopts(recast(rcap) lwidth(medthick))  ///
   xline(3.5, lcolor(red)) yline(0, lcolor(gs10) lpattern(dash)) xtitle("Years to Posting Liberalization") ytitle(Log Posted Workers Flows) ///
   legend(ring(0) pos(5) cols(1)) ylabel(-4 (2) 4)
   graph export "$replication_outputs_a/FigureB12.pdf", replace
   
   ****************************************************************
   * Heterogeoneity by timing of liberalization (appendix figures)
   ****************************************************************
   g indic_mig=0
   replace indic_mig=1 if destination=="DE"
   replace indic_mig=1 if destination=="AT"
 
   
   reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_19 if indic_mig==1, absorb(pair year destinationnum##year) cl(pair)
   est store desti1
   reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up if indic_mig==0, absorb(pair year destinationnum##year) cl(pair)
   est store desti2 
   
   	
   coefplot (desti2, label("Posting liberalized before free movement")) ///
   (desti1,  label("Posting is liberalized same year than free movement")) ///
   ,keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22 ) ///
   omit vertical ///
   graphregion(fcolor(white) lcolor(white))   ciopts(recast(rcap) lwidth(medthick))  ///
   xline(4.5, lcolor(red)) yline(0, lcolor(gs10) lpattern(dash)) xtitle("Years to Posting Liberalization") ytitle(Log Posted Workers Flows) ///
   legend(ring(0) pos(5) cols(1)) ylabel(-4 (2) 4)
   graph export "$replication_outputs_a/FigureB8.pdf", replace

   

   


 *SAME DATASET BUT YEAR IS NOW MIGRATION REFORM
  import excel "$data/EUaccession_ES.xlsx", sheet("mig") firstrow clear
 g treated=0
 replace treated=1 if EVENT>0
 drop if country=="total"

 destring n*, replace force
 reshape long n, i(country destination NMS1 NMS2 NMS3 treated EVENT) j(year)
 drop NMS1 NMS2 NMS3
 g time=year-EVENT
 replace time=0 if treated==0
 sum time if treated==1
 global min=abs(r(min))
 
 g timed=time+$min
 replace timed=0 if treated==0
 su timed
 global max=abs(r(max))
 
 forval y=0/$max{
 g coeff_`y'=0
 replace coeff_`y'=1 if treated==1 & timed==`y'
 }
 
forval y=0/$max{
local j=`y'-$min
label var coeff_`y' "`j'"
}


g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4

g coeff_up=0
replace coeff_up=1 if treated==1 & time>8
g logposted=log(n)

egen pair=group(destination country)
egen countrynum=group(country)
egen destinationnum=group(destination)


g zero=1

   label var zero "-1"
   reghdfe logposted coeff_bottom coeff_15-coeff_17 zero coeff_19-coeff_27 coeff_up, absorb(pair year destinationnum##year) cl(pair)
   coefplot, keep(coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_15 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20  coeff_21 coeff_22 coeff_23 coeff_24 coeff_25 coeff_26 coeff_27 ) omit vertical ///
   graphregion(fcolor(white) lcolor(white))   ciopts(recast(rcap) lwidth(medthick)) ///
   scale(1.2)  xline(4.5, lcolor(red)) yline(0, lcolor(gs10) lpattern(dash)) xtitle("Years to Migration Liberalization") ytitle(Log Posted Workers Flows) ylabel(-4 (2) 4)
    graph export "$replication_outputs_a/FigureB7.pdf", replace

   reghdfe logposted coeff_bottom coeff_15-coeff_17 zero coeff_19-coeff_27 coeff_up, absorb(pair year ) cl(pair)
   coefplot, keep(coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_15 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20  coeff_21 coeff_22 coeff_23 coeff_24 coeff_25 coeff_26 coeff_27 ) omit vertical ///
   graphregion(fcolor(white) lcolor(white))   ciopts(recast(rcap) lwidth(medthick)) ///
   scale(1.2)  xline(4.5, lcolor(red)) yline(0, lcolor(gs10) lpattern(dash)) xtitle("Years to Migration Liberalization") ytitle(Log Posted Workers Flows) ylabel(-4 (2) 4)
   
      
   
 
	